package Ioperations;

import book.Bookshelf;

import java.util.Scanner;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 28318
 * Date: 2025-10-28
 * Time: 17:33
 */
public class DeleteIoperation implements Ioperations{
    @Override
    public void work(Bookshelf book) {
        System.out.println("***>删除图书...<***");
        if (book.getBookUsed() == 0){
            System.out.println("书架中没有图书,无法删除...");
            return;
        }
        Scanner inPut = new Scanner(System.in);
        System.out.println("请输入要删除的图书名:");
        String name = inPut.nextLine();
        int address = -1;
        for (int i = 0; i < book.getBookUsed(); i++) {
            if(book.bookshelf[i].getName().equals(name)){
                address = i;
                break;
            }
        }
        if (address == -1){
            System.out.println("未找到您要删除的图书...");
            return;
        }
        for (int i = address; i < book.getBookUsed() - 1; i++) {
            book.bookshelf[i] = book.bookshelf[i + 1];
        }
        book.setBookUsed(book.getBookUsed() - 1);
        System.out.println("删除成功...");
    }
}
